import { computed } from 'vue';
import dayjs from 'dayjs';

/**
 * 查询条件数据处理
 *
 * @param {Object} searchParams 查询条件
 * @param {Array} timeArr 时间数组
 * @param {String} format 时间格式
 * @returns {Object} 使用计算属性查询条件数据
 */
export default function useSearchData(
  searchParams: any,
  timeArr = ['startTime', 'endTime'],
  format = 'YYYY-MM-DD HH:mm:ss'
) {
  const searchData = computed(() => {
    const params = {
      ...searchParams.value
    };
    if (searchParams.value?.time?.[0]) {
      params[timeArr[0]] = dayjs(searchParams.value.time[0]).format(format);
      params[timeArr[1]] = dayjs(searchParams.value.time[1]).format(format);
    }
    delete params.time;
    return params;
  });

  return searchData;
}
